$(document).ready(function() {	
	findAllUserState();
	/*
	findAllRole();	
	findAllDeptType();
	findAllUserState()
	$("#deptType").change(function() {
		var dtId = $("#deptType option:selected").val();
		$.ajax({
  			type: "post",
  			data:{"dtId":dtId},
 			url: "admin/userDept_findUserDeptByDtId.action",
  			success:function(data){
  				$("#allDept").empty();
  				$.each(data.userDepts,function(i,n){
  					$("<option value="+n.udId+">"+n.udName+"</option>").appendTo("#allDept");
				});
  			}
  		});
	});
	
	$("#addDept").click(function() {
		if ($("#allDept option:selected").length > 0) {
			$("#allDept option:selected").each(function() {
				$("#selDept").append("<option class='class1' name='selDept' selected value='" + $(this).val() + "'>"
						+ $(this).text() + "</option");
				$(this).remove();
			})
		}
	});
	
	$("#delDept").click(function() {
		if ($("#selDept option:selected").length > 0) {
			$("#selDept option:selected").each(function() {
				$("#allDept").append("<option class='class1' value='" + $(this).val() + "'>"
						+ $(this).text() + "</option");
				$(this).remove();
			})
		}
	});
	
	$("#addLeadDept").click(function() {
		if ($("#selDept option:selected").length > 0) {
			$("#selDept option:selected").each(function() {
				var udId = $(this).val();
				var udName = $(this).text();
				if ($("#leadDept option").length > 0) {
					var t = true;
					$("#leadDept option").each(function(){
						if(udId == $(this).val()){ 
							t = false;
						}
					})
					if(t){
						$("#leadDept").append("<option class='class1' name='leadDept' selected value='" + udId + "'>"
							+ udName + "</option");
					}
				}else{
					$("#leadDept").append("<option class='class1' name='leadDept' selected value='" + udId + "'>"
						+ udName + "</option");
				}
			})
		}
	});
	
	$("#delLeadDept").click(function() {
		if ($("#leadDept option:selected").length > 0) {
			$("#leadDept option:selected").each(function() {
					$(this).remove();
			})
		}
	});
	$("#addRole").click(function() {
		if ($("#allRole option:selected").length > 0) {
			$("#allRole option:selected").each(function() {
				$("#selRole").append("<option class='class1' name='selRole' selected value='" + $(this).val() + "'>"
						+ $(this).text() + "</option");
				$(this).remove();
			})
		}
	});
	
	$("#delRole").click(function() {
		if ($("#selRole option:selected").length > 0) {
			$("#selRole option:selected").each(function() {
				$("#allRole").append("<option class='class1' value='" + $(this).val() + "'>"
						+ $(this).text() + "</option");
				$(this).remove();
			})
		}
	});*/
	
	var uId = $("#uId"),
		uName = $("#uName"),
		uPassword = $("#uPassword"),
		pwd_check = $("#pwd_check"),
		uPhone = $("#uPhone"),
		uTel = $("#uTel"),
		uFax = $("#uFax"),
		uMail = $("#uMail"),
		uRole = $("#uRole")
	initvalidator();
	$("#userForm").validate({
		//onsubmit: false,
		//onkeyup: false,
		errorClass: "invalid",
		submitHandler: function(form){   //表单提交句柄,为一回调函数，带一个参数：form  
			var depts = $("#ud_udIds").text();
			var roles = $("#r_rIds").text();
			if(r_rIds==""){
				alert("所属角色一定要选择！");
				return;
			}
			if(ud_udIds==""){
				alert("所属部门一定要选择！");
				return;
			}
			if(depts=="00")
				depts=""
			if(roles=="00")
				roles=""
       		$.ajax( {
				type : "post",
				data : {"user.UId":uId.text(),"user.UName":uName.val(),
       					"user.UPassword":uPassword.val(),"user.UPhone":uPhone.val(),
       					"user.UTel":uTel.val(), "user.UFax":uFax.val(),"user.UMail":uMail.val(),  
       					"selDepts":depts,"selRoles":roles,"user.tbUserState.usId":$("#userStatus").val()
						 },
				url : "admin/user_update.action",
				success : function(data){
					 if(data=="true"){
					 	alert("修改用户成功！");
					 	$('#tabFrame', window.parent.document).attr("src", "/ag/admin/users.jsp");
					 }else{
					 	$("#message").text("修改失败！").css("color","red");
						showMessage();
					 }
				}
			});
    	}, 
		rules: {
			'user.UId':{
    			required:true,
    			remote:{
    				url: "admin/user_checkUId.action",
   					type: "post"
    			}
    		},
			'user.UName':{
    			required:true,
    			minlength: 2
    		},
			'pwd_check': {  
            	equalTo: "#uPassword"  
       		 },
       		'user.UPhone':{//       		 	isMobile:true
       			required:true,
       			digits:true,
       			minlength: 2,
       			maxlength: 12
       		 },
       		 'user.UTel':{//isTel:true
//       			required:true,
       			digits:true,
       			minlength: 2,
       			maxlength: 18
       		 },
       		 'user.UFax':{//isTel:true
//       			required:true,
       			digits:true,
       			minlength: 2,
       			maxlength: 18
       		 },
       		 'user.UMail':'email'
		},
		messages: {
		'user.UId':{
			required:'必须填写',
			remote:'已经被占用'
		},
		'user.UName':{
			required:'必须填写',
			minlength:'不小于两个字',
			remote:'remote'
		},
		'pwd_check':"密码要一致",
		'user.UPhone':"号码长度2~18",
		'user.UTel':"号码长度2~18",
		'user.UFax':"号码长度2~18",
		'user.UMail':'如：pat@example.com '
		}
	});
	
});	
//	//获取所有角色
//	function findAllRole(){
//		$.ajax({
//  			type: "post",
// 			url: "admin/Role_findAllRole.action",//因为com.ag.admin.action.RoleAction的controller配的是RoleAction
//  			success:function(data){
//  				$("<option value='0'>请选择</option>").appendTo(".uRole");
//  				$.each(data.roles,function(i,n){
//  					$("<option value="+n.RId+">"+n.RName+"</option>").appendTo(".uRole");
//  					//数据添加到添加用户时选择角色的地方
//  					$("<option value="+n.RId+">"+n.RName+"</option>").appendTo("#allRole");
//				});
//  			}
//  		});
//	}
//	
//	//获取所有部门类型
//	function findAllDeptType(){
//		$.ajax({
//  			type: "post",
// 			url: "admin/userDept_findAllDeptType.action",
//  			success:function(data){
//  				$("<option value='0'>请选择</option>").appendTo("#deptType");
//  				$.each(data.deptTypes,function(i,n){
//  					$("<option value="+n.dtId+">"+n.dtName+"</option>").appendTo("#deptType");
//				});
//  			}
//  		});
//	}

	//获取所有用户状态
	function findAllUserState(){
		$.ajax({
  			type: "post",
 			url: "admin/user_findAllUserState.action",
  			success:function(data){
  				var usId = $("#usId").val();
  				$.each(data.userStates,function(i,n){
  					if(usId == n.usId)
  						$("<option value="+n.usId+" selected>"+n.usName+"</option>").appendTo("#userStatus");
  					else
  						$("<option value="+n.usId+">"+n.usName+"</option>").appendTo("#userStatus");
				});
  			}
  		});
	}
			/**
	 * 显示出错信息
	 * @memberOf {TypeName} 
	 */
	function showMessage(){
		$("#dialog").dialog("destroy");
		$("#dialog-message").dialog({
			height:200,
			width: 300,
			modal: true,
			resizable: false,
			buttons: {
				Ok: function() {
					$(this).dialog('close');
				}
			}
		});
	}
		
function initvalidator() {
	
	jQuery.validator.addMethod("chinese", function (value, element) {
		//alert("chinese validator");
		var chinese = /^[\u4e00-\u9fa5]+$/;
		return this.optional(element) || chinese.test(value);
	}, "\u8bf7\u8f93\u5165\u4e2d\u6587");
	jQuery.validator.addMethod("isMobile", function (value, element) {
		//alert("Mobile validator");
		var length = value.length;
		var mobile = /^\d{1,18}$/;
		return this.optional(element) || (length == 11 && mobile.test(value));
	}, "\u8bf7\u6b63\u786e\u586b\u5199\u60a8\u7684\u624b\u673a\u53f7\u7801"); 
	
	// 电话号码验证      
	jQuery.validator.addMethod("isTel", function (value, element) {
		//alert("Tel validator"); 
		var tel = /^\d{1,18}$/;    //电话号码格式010-12345678  
		return this.optional(element) || (tel.test(value));
	}, "\u8bf7\u6b63\u786e\u586b\u5199\u60a8\u7684\u7535\u8bdd\u53f7\u7801,\u683c\u5f0f010-12345678");
	
	// 联系电话(手机/电话皆可)验证  
	jQuery.validator.addMethod("isPhone", function (value, element) {
		//alert("Phone validator");
		var length = value.length;
		var mobile = /^\d{1,18}$/;
		var tel = /^\d{1,18}$/;
		return this.optional(element) || (tel.test(value) || mobile.test(value));
	}, "\u8bf7\u6b63\u786e\u586b\u5199\u60a8\u7684\u8054\u7cfb\u7535\u8bdd");
}